package my_a10bbc.cn.xp.com.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;



import java.util.ArrayList;

import my_a10bbc.cn.xp.com.bean.IQuesBean;
import sdstcom.lbq.one.R;

/**
 * Created by Administrator on 2017/1/10.
 */
public class IQuesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{
    private Context context;
    private ArrayList<IQuesBean.DataBean> list;
    private LayoutInflater inflater;
    private RecyclerView rv;
    private String type;

    private MyOnItemclick click;

    public IQuesAdapter(Context context, ArrayList<IQuesBean.DataBean> list) {
        this.context = context;
        this.list = list;
        inflater=LayoutInflater.from(context);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh = null;//
        View view=inflater.inflate(R.layout.essay_recycler_item,parent,false);
        view.setOnClickListener(this);
        vh=new ViewHolder(view);

        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh= (ViewHolder) holder;
        IQuesBean.DataBean obj=list.get(position);
        Log.e("TS",obj.question_title );
        vh.tv1.setText(obj.question_title);
        vh.tv2.setText(obj.answer_title);
        vh.tv3.setText(obj.answer_content);
        vh.tv4.setText("问答");
    }

    @Override
    public int getItemCount() {
        return list==null ? 0:list.size();

    }

    public interface MyOnItemclick{
        void onItemClick(int index);
    }
    public void setOnItemClick(MyOnItemclick click) {
        this.click = click;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        rv=recyclerView;
    }


    //点击事件
    @Override
    public void onClick(View v) {
        if(click!=null){
            click.onItemClick(rv.getChildAdapterPosition(v));
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tv1,tv2,tv3,tv4;

        public ViewHolder(View itemView) {
            super(itemView);
            tv1=(TextView)itemView.findViewById(R.id.tv1_essay_recycler_item);
            tv2=(TextView)itemView.findViewById(R.id.tv2_essay_recycler_item);
            tv3=(TextView)itemView.findViewById(R.id.tv3_essay_recycler_item);
            tv4=(TextView) itemView.findViewById(R.id.btn_essay_recycler_item);

        }
    }
}
